package za.ac.wits.elen7045.persistence.datasource.sqlite;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import za.ac.wits.elen7045.persistence.datasource.DataSourceRowEntry;
import za.ac.wits.elen7045.persistence.model.Credential;
import za.ac.wits.elen7045.persistence.util.AccountTableConstants;
import za.ac.wits.elen7045.persistence.util.CredentialsTableConstants;
import za.ac.wits.elen7045.persistence.util.ServiceProviderTableConstants;

public class CredentialSQLiteDataSource extends SQLiteDataSource<Credential> implements CredentialsTableConstants {

	private final String ACCOUNT_ID = AccountTableConstants.ACCOUNT_ID;
	private final String SERVICE_PROVIDER_ID = ServiceProviderTableConstants.SERVICE_PROVIDER_ID;
	
	@Override
	protected Credential parseCurrentRowToObject(ResultSet resultSet) throws SQLException {
		Credential credential = new Credential();
		if (isAccountCredential(resultSet)) {
			credential.setOwnerId(resultSet.getInt(ACCOUNT_ID));
			credential.setId(resultSet.getInt(CREDENTIAL_ID));
			credential.setName(resultSet.getString(CREDENTIAL_NAME));
			credential.setValue(resultSet.getString(CREDENTIAL_VALUE));
			credential.setEncrypted(resultSet.getString(CREDENTIAL_ENCRYPTED).equalsIgnoreCase(Boolean.TRUE.toString()));
		} else if (isServiceProviderCredential(resultSet)){
			credential.setOwnerId(resultSet.getInt(SERVICE_PROVIDER_ID));
			credential.setId(resultSet.getInt(CREDENTIAL_ID));
			credential.setName(resultSet.getString(CREDENTIAL_NAME));
		}
		return credential;
	}

	@Override
	protected DataSourceRowEntry parseObjectToDataSourceRowEntry(Credential credential) throws SQLException {
		DataSourceRowEntry entry = new DataSourceRowEntry();
		if (isAccountCredential(resultSet)) {
			entry.setValue(ACCOUNT_ID, credential.getOwnerId());
			entry.setValue(CREDENTIAL_ID, credential.getId());
			entry.setValue(CREDENTIAL_NAME, credential.getName());
			entry.setValue(CREDENTIAL_VALUE, credential.getValue(false));
			entry.setValue(CREDENTIAL_ENCRYPTED, Boolean.toString(credential.isEncrypted()));
		} else if (isServiceProviderCredential(resultSet)){
			entry.setValue(SERVICE_PROVIDER_ID, credential.getOwnerId());
			entry.setValue(CREDENTIAL_ID, credential.getId());
			entry.setValue(CREDENTIAL_NAME, credential.getName());
		}
		return entry;
	}
	
	/* Utilities */
	
	private boolean isAccountCredential(ResultSet resultSet) {
		try {
			ResultSetMetaData metaData = resultSet.getMetaData();
			String tableName = metaData.getColumnCount() > 0 ? metaData.getTableName(1) : null;
			return ACC_CRED_TABLE.equals(tableName);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	private boolean isServiceProviderCredential(ResultSet resultSet) {
		try {
			ResultSetMetaData metaData = resultSet.getMetaData();
			String tableName = metaData.getColumnCount() > 0 ? metaData.getTableName(1) : null;
			return SERV_PROV_CRED_TABLE.equals(tableName);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
